﻿/**
* 类型(构造函数)
*/
export interface type<T> {
    new (): T
}

/**
* 新建Guid
*/
export function newGuid(): string {
    let guid = "{";
    for (let i = 1; i <= 32; i++) {
        let n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";

    }
    return guid + "}";
}
/**
* 获取当前日期字符串（精确到日）
*/
export function getCurrentDateString(): string {
    let today = new Date();
    let timeString = today.getFullYear() + "/" + today.getMonth() + "/" + today.getDay();
    return timeString;
}

/**
* 获取当前时间字符串（精确到毫秒）
*/
export function getCurrentTimeString(): string {
    let today = new Date();
    return `${today.getFullYear()}/${today.getMonth()}/${today.getDay()} ${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}.${today.getMilliseconds()}`;
}

/**
 * 代理
 */
export class proxy {
    /**
    * 创建透明代理
    * @param origionObject 原始对象
    * @param methodHandler 代理方法
    */
    static createTransparentProxy(origionObject: new () => any, methodHandler: (methodName: string, args: any[]) => any): any {
        let proxyObject = {};
        let proxyType = origionObject.prototype;
        reflect.assignType(proxyObject, origionObject);
        let funcNameList: string[] = [];
        // 将函数名赋值到数组，进行闭包处理
        for (var func in proxyType) {
            var prop = proxyType[func];
            if (prop instanceof Function) {
                funcNameList.push(func);
            }
        }
        // 通过函数名数组定义代理的方法
        for (let funcName of funcNameList) {
            proxyObject[funcName] = (...proxyArgs: any[]): any => {
                return methodHandler(funcName, proxyArgs);
            }
        }

        return proxyObject;
    }
}

/**
 * 反射
 * @remark 根据javascrip原型概念实现反射功能
 */
export class reflect {
    /**
    * 指定类型
    * @param originObject 要指定类型的原始对象
    * @param targetType 目标类型
    */
    static assignType(originObject: any, targetType: Function) {
        let newMethod: any = targetType;
        originObject.__proto__ = newMethod.prototype;
        originObject.__proto__.constructor = newMethod;

        // 调用构造方法，并复制数据到原始对象
        let newObj = new newMethod();
        reflect.propertyCopy(originObject, newObj);

        // 调用事件
        if (originObject.onAssignedType) {
            originObject.onAssignedType();
        }
    }
    /**
    * 获取类名
    * @param objClass 对象类型
    */
    static getTypeFullName(objClass: any): string {
        let func: Function;
        if (objClass instanceof Function) {
            func = objClass;
        }
        else if (objClass && objClass.constructor) {
            func = objClass.constructor;
        }
        if (func) {
            let strFun = func.toString();
            let className = strFun.substr(0, strFun.indexOf('('));
            className = className.replace('function', '');
            className = className.replace(/(^\s*)|(\s*$)/ig, '');
            if (func.prototype.$module) {
                return func.prototype.$module + "." + className;
            } else {
                return className;
            }
        }
        if (objClass.name) {
            return objClass.name;
        }
        return typeof objClass;
    }

    /**
        * 属性赋值，将源对象的所有属性复制到目标对象
        * @param destObject 目标对象
        * @param srcObject 源对象
        * @param coverDefinedField 覆盖已经有数值的字段
        */
    static propertyCopy(destObject: Object, srcObject: Object, coverDefinedField: boolean = false): void {
        for (let property in srcObject) {
            // 只复制没有设置的属性
            if (coverDefinedField || !destObject[property]) {
                destObject[property] = srcObject[property];
            }
        }
    }
}

/**
* 用户令牌
*/
export class userToken {
    /**
    * 用户名
    */
    name: string;
    /**
    * 密码，经过MD5运算
    */
    password: string;
}

/**
* 当前用户
*/
export let currentUser: userToken;

/**
* Json请求
*/
export interface remoteHeader {
    userID?: string;
    transactionID?: string;
    password?: string;
}

/**
* 远程请求
*/
export interface remoteRequest {
    serviceName: string;
    functionName: string;
    header?: remoteHeader;
    args?: any;
}